var moment = require('moment');
var db = require('../config/mongo_database');
var secret = require('../config/secret');
var redisClient = require('../config/redis_database').redisClient;
var tokenManager = require('../config/token_manager');

/*******************
  实物商品可以修改订单信息，添加物流等信息，更改收货状态等

*******************/

// 查询
exports.list = function(req, res){
	var status = req.body.status || '';
	var type = req.body.type || '';
	var pages = req.body.pages || 0;
	
	var where = {};
	if(status!='') where.status = status;
	if(type!='') where.type = type;

	console.log(where)
	db.orderModel.count(where, function(err, count){
		db.orderModel
		.where(where)
		.skip(pages*12)
		.limit(12)
		.sort({ updated : -1 })
		.exec(function(err, list){
			res.json({count:count, list:list});
		})
	}).sort({'updated':-1});
}

// 取消订单
exports.cancelOrder = function(req, res){
	var orderid = req.query.orderid || '';
	var whereData = {orderid:orderid, status:"0"};
	db.orderModel.remove(whereData, function(err){ 
		return res.sendStatus(200); 
	})
}

// 更新订单
exports.update = function(req, res){
	var data = req.body;
	console.log(data)
	if(data.status=='' || data.status==null || data.status=='0')
		return res.sendStatus(403); 
	db.orderModel.update({orderid:data.orderid}, 
		{$set:{status:data.status, express:data.express, logs:data.logs, outtrade:data.outtrade, openAmount:data.openAmount, updated:new Date()}}, function(err){ 
		return res.sendStatus(200); 
	})
}
